﻿Public Class AlarmSoundForm

    Private Sub AlarmSoundForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.txtFileName.ReadOnly = True
        'Me.txtFileName.BackColor = Color.White
        LoadConfig()
    End Sub

    Private Sub LoadConfig()
        Me.txtFileName.Text = g_AlarmSoundFile

        Dim iDisable As Integer = 0
        iDisable = KeyValuePairClass.GetValue(MainForm.pi.ProjectID, "DisableAlarmSound")
        If iDisable = 1 Then
            Me.chkEnableSound.Checked = True
            Me.txtFileName.Text = ""
            Me.btnBrowser.Enabled = False
        Else
            Me.chkEnableSound.Checked = False
        End If
    End Sub

    Private Sub btnBrowser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowser.Click
        Dim dlg As New OpenFileDialog
        dlg.InitialDirectory = Application.StartupPath
        dlg.Filter = "wav文件(*.wav)|*.wav|mp3文件(*.mp3)|*.mp3|mid文件(*.mid)|*.mid"
        If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.txtFileName.Text = dlg.FileName
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If checkValid() = False Then
            Exit Sub
        End If

        If Save() Then
            msg.Show("保存成功")
        Else
            msg.ShowError("保存失败")
        End If
    End Sub

    Private Function CheckValid() As Boolean
        If Me.chkEnableSound.Checked = False AndAlso Me.txtFileName.Text.Trim() = "" Then
            msg.Show("请选择报警声音文件")
            Me.btnBrowser.Focus()
            Return False
        End If

        Return True

    End Function

    Private Sub chkEnableSound_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEnableSound.CheckedChanged
        If Me.chkEnableSound.Checked Then
            Me.btnBrowser.Enabled = False
            Me.btnPlay.Enabled = False
            Me.txtFileName.Text = ""
        Else
            Me.btnBrowser.Enabled = True
            Me.btnPlay.Enabled = True
        End If
    End Sub

    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Dim fileName As String = Me.txtFileName.Text.Trim()
        If System.IO.File.Exists(fileName) Then
            'My.Computer.Audio.Play(fileName)
            TBCC.Utility.Media.PlayerHelper.Instance().Play(fileName)
        End If
    End Sub


    Private Function Save() As Boolean
        g_AlarmSoundFile = Me.txtFileName.Text.Trim()
        KeyValuePairClass.SetValue(MainForm.pi.ProjectID, "AlarmSoundFile", g_AlarmSoundFile)

        Dim iDisable As Integer = 0
        If Me.chkEnableSound.Checked Then
            iDisable = 1
        Else
            iDisable = 0
        End If
        KeyValuePairClass.SetValue(MainForm.pi.ProjectID, "DisableAlarmSound", iDisable)

        Return True
    End Function

End Class